import { getTopCategoryAPI } from "@/apis/category";
import { onMounted, ref } from "vue";
import { onBeforeRouteUpdate, useRoute } from "vue-router";

export function useCategory() {
  const categoryData = ref({});
  const route = useRoute();
  
  // 使用路由导航准确更新
  onBeforeRouteUpdate((to)=> {
    getCategory(to.params.id)
  })
  
  // 添加参数
  const getCategory = async (id = route.params.id) => {
    // 获取路由参数，如果是:/id就用route.params.id
    const res = await getTopCategoryAPI(id);
    categoryData.value = res.result;
  };
  
  onMounted(() => {
    getCategory();
  });

  return {
    categoryData
  }
}
